package com.wyj.session;

import com.wyj.config.SqlCommandType;
import com.wyj.executor.SimpleExecutor;
import com.wyj.pojo.Configuration;
import com.wyj.pojo.MapperedStatement;

import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

/**
 * @author wuyijie
 * @description: TODO 其操作，主要通过executor完成
 * @date 2021/10/30 2:06 下午
 */
public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws SQLException, NoSuchFieldException, ClassNotFoundException {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MapperedStatement mapperedStatement = configuration.getMapperedStatementMap().get(statementId);
        List<Object> query = simpleExecutor.query(configuration, mapperedStatement, params);
        return (List<E>) query;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws SQLException, NoSuchFieldException, ClassNotFoundException {
        List<Object> objects = this.selectList(statementId, params);
        if (objects.size() > 1) {
            throw new RuntimeException("期望一个结果，返回了多个");
        } else {
            if (objects.size() == 1) {
                return (T) objects.get(0);
            }
            return null;
        }
    }

    @Override
    public int update(String statementId, Object... param) throws SQLException, NoSuchFieldException, ClassNotFoundException {
        int update = 0;
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MapperedStatement mapperedStatement = configuration.getMapperedStatementMap().get(statementId);
        update = simpleExecutor.update(configuration, mapperedStatement, param);
        return update;
    }


    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        Object o = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            //代理对象。具体执行的方法对象。参数。
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //获取statementid
                String methodName = method.getName();
                //方法所属类型全限定类名
                String className = method.getDeclaringClass().getName();
                String key = className + "." + methodName;
                //获取方法返回值类型
                Type genericReturnType = method.getGenericReturnType();

                MapperedStatement mapperedStatement = configuration.getMapperedStatementMap().get(key);
                SqlCommandType sqlCommandType = mapperedStatement.getSqlCommandType();
                if (sqlCommandType.equals(SqlCommandType.SELECT)) {
                    if (genericReturnType instanceof ParameterizedType) {
                        return selectList(key, args);
                    }
                    return selectOne(key, args);
                } else {
                    return update(key, args);
                }
            }
        });
        return (T) o;

    }
}
